<template>
  <div class="container" v-loading="isLoading">
    <el-card>
      <template #header>
        <el-form inline :model="searchForm" ref="searchForm">
          <el-form-item label="支部名称" prop="news_type">
            <el-select style="width:500px"  v-model="builingForm.party_user_type_id" placeholder="请选择支部名称">
              <el-option label="全部" value="" />
              <el-option
                v-for="(oVal, oIdx) of type"
                :key="oIdx"
                v-bind="oVal"
              />
            </el-select>
          </el-form-item>

          <!-- <el-form-item label="姓名" prop="name">
            <el-input v-model="searchForm.name" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <el-select
              v-model="searchForm.gender"
              placeholder="请选择党员性别"
            >
              <el-option
                v-for="(oVal, oIdx) of builingOptions.sex"
                :key="oIdx"
                v-bind="oVal"
              />
            </el-select>
          </el-form-item> -->

          <el-form-item>
            <el-button type="primary" @click="submitSearch">查询</el-button>
          </el-form-item>
        </el-form>
        <div class="table_tool">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="showAddOrSolve = true"
            >新增</el-button
          >
        </div>
      </template>

      <el-table :data="tableData" border stripe>
        <el-table-column prop="real_name" label="姓名" />
        <el-table-column prop="avatar_url" label="头像">
          <template #default="{ row }">
            <el-image
              style="width: 100px; height: 130px"
              :src="row.avatar_url"
              :preview-src-list="[row.avatar_url]"
            />
          </template>
        </el-table-column>
        <el-table-column prop="sex" label="性别">
          <template slot-scope="{ row }">
            <div v-if="row.sex=='1'">男</div>
            <div v-else>女</div>
          </template>
        </el-table-column>

        <el-table-column prop="job" label="党内职务" />
        <el-table-column prop="phone" label="手机号" />
        <el-table-column prop="email" label="是否点亮" >
          <template slot-scope="{row}">
            <div v-if="row.user_active">激活</div>
            <div v-else>未激活</div>
          </template>
        </el-table-column>

        <el-table-column prop="summary" label="党员寄语" show-overflow-tooltip/>
        <el-table-column prop="age" label="年龄" />
        <el-table-column prop="city" label="籍贯" />
        <el-table-column prop="join_party_time" label="入党时间" />
        <el-table-column label="操作" width="120px" align="center">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              @click="openEdit(row)"
              >编辑</el-button
            >
            <el-button type="text" class="red" @click="deleteById(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <footer class="footer">
      <el-pagination
        background
        layout="total, sizes, pager, jumper"
        :current-page="searchForm.page"
        :page-sizes="[10, 20, 30, 50, 100]"
        :page-size="searchForm.page_size"
        :total="tableTotal"
        @size-change="changeSize"
        @current-change="changeCurrent"
      />
    </footer>

    <el-dialog
      :title="this.builingForm.$id?'编辑党员':'新增党员'"
      :visible.sync="showAddOrSolve"
      width="1400px"
      @closed="resetForm()"
    >
      <el-form
        :model="builingForm"
        ref="builingForm"
        label-width="7em"
      >
        <el-form-item prop="$id" hidden />

        <el-form-item class="span-3" label="党员名称" prop="real_name">
          <el-input
            v-model="builingForm.real_name"
            placeholder="请输入党员名称"
          ></el-input>
        </el-form-item>

        <el-form-item class="span-3" label="党员籍贯" prop="city">
          <el-input
            v-model="builingForm.city"
            placeholder="请输入党员籍贯"
          ></el-input>
        </el-form-item>

        <el-form-item class="span-3" label="党员年龄" prop="age">
          <el-input minlength="11"
            v-model="builingForm.age"
            placeholder="请输入党员年龄"
          ></el-input>
        </el-form-item>

        <el-form-item class="span-3" label="党员手机号" prop="phone">
          <el-input minlength="11"
            v-model="builingForm.phone"
            placeholder="请输入党员手机号"
          ></el-input>
        </el-form-item>

        <el-form-item class="span-3" label="入党时间" prop="party_day">
          <el-input
            v-model="builingForm.party_day"
            placeholder="请输入入党时间"
          ></el-input>
        </el-form-item>

         <el-form-item class="span-3" label="党内职位" prop="job">
          <el-input
            v-model="builingForm.job"
            placeholder="请输入党内职位"
          ></el-input>
        </el-form-item>

        <el-form-item class="span-2" label="党员性别" prop="sex">
          <el-select v-model="builingForm.sex" placeholder="请选择性别">
            <el-option
              v-for="(oVal, oIdx) of sexObje"
              :key="oIdx"
              v-bind="oVal"
            />
          </el-select>
        </el-form-item>

        <el-form-item class="span-2" label="所在支部" prop="party_user_type_id">
          <el-select style="width:500px"   v-model="builingForm.party_user_type_id" placeholder="请选择支部">
            <el-option
              v-for="(oVal, oIdx) of type"
              :key="oIdx"
              v-bind="oVal"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="党员寄语" prop="summary">
          <el-input
            v-model="builingForm.summary"
            type="textarea"
            row="3"
            placeholder="请输入党员寄语"
          ></el-input>
        </el-form-item>

        <el-form-item label="党员头像图片" prop="avatar_url">
          <Upload
            v-model="builingForm.avatar_url"
            :file.sync="builingForm.avatar_url"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button
          type="primary"
          @click="addParter()"
          :loading="loading.update"
          >提交</el-button
        >
      </template>
    </el-dialog>


  </div>
</template>

<script>
import API from '@/api';
import { table, addOrEdit } from "@/utils/mixins";
import Upload from "@/components/Upload/SingleImage.vue";

export default {
  mixins: [table, addOrEdit],
  components: {
    Upload,
  },

  data: () => ({
    showAddOrSolve:false,//编辑
    isLoading: true,
    tableData:[],
    tableTotal: 0,
    isKey: false,
    searchForm: {
      party_user_type_id:'',
      page: 1, // 当前页码
      per_page: 10, // 每页条数
    },

    builingForm: {
      $id:"",
      real_name:"",
      city:"",
      age:"",
      phone:"",
      sex:"",
      job:"",
      party_day:"",
      summary:"",
      avatar_url:"",
      party_user_type_id:"",
    },
    sexObje:[
      {label:'男',value:'1'},
      {label:'女',value:'2'},
    ],
    type:[],
    communityType:[],

  }),

  created() {
    this.isLoading=true
    API.getPartyType()
      .then(({data}) => {
        let type=data.map(item=>{
          return {label:item.name,value:item.id}
        })
        this.type=type
        this.getList()
      }).catch((err) => {
      }).finally(() =>{
      })
  },

  methods: {
    // 获取列表
    getList(){
      this.isLoading=true
      let id=this.searchForm.party_user_type_id
      API.getPartyMemberList(id,this.searchForm)
        .then(({data}) => {
          data.items.forEach(item=>{
            item.phone=item.phone.replace(/^(\d{3})\d{4}(\d+)/, '$1****$2')
          })
          this.tableData=data.items
          this.tableTotal=data.total
        }).catch((err) => {
          this.tableData=[]
          this.tableTotal=0
        }).finally(() =>{
          this.isLoading = false;
        })
    },

    // 分页
    changeSize(val){
      this.searchForm.per_page = val
      this.searchForm.page = 1
      this.getList()
    },

    // 分页
    changeCurrent(val){
      this.searchForm.page = val
      this.getList()
    },

    // 删除
    deleteById(id){
      let _that=this
      this.$confirm(`是否删除这条党员数据？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
      .then(() => {
        API.delPartyMember(id)
        .then(({data}) => {
          this.$message.success("删除成功！");
        }).catch((err) => {
          this.$message.error("网络异常~");
        }).finally(() =>{
          _that.getList()
        })
      })
      .catch(() => {
      });
    },

      // 编辑活动
    openEdit(obj){
      this.builingForm.$id=obj.id
      this.builingForm.real_name=obj.real_name
      this.builingForm.city=obj.city
      this.builingForm.age=obj.age
      this.builingForm.phone=obj.phone
      this.builingForm.sex=obj.sex
      this.builingForm.job=obj.job
      this.builingForm.party_day=obj.party_day
      this.builingForm.summary=obj.summary
      this.builingForm.avatar_url=obj.avatar_url
      this.builingForm.party_user_type_id=obj.party_user_type_id
      this.showAddOrSolve=true
    },

    //修改党员
    addParter(){
      if(!this.builingForm.real_name.trim()) return this.$message.error("党员姓名不可为空~~");
      // if(!this.builingForm.city.trim()) return this.$message.error("党员籍贯不可为空~~")
      // if(!this.builingForm.age.trim()) return this.$message.error("党员年龄不可为空~~")
      if(!this.builingForm.phone.trim()) return this.$message.error("党员电话不可为空~~")
      // if(!this.builingForm.party_day.trim()) return this.$message.error("入党日期不可为空~~")
      if(!this.builingForm.job.trim()) return this.$message.error("党内职务不可为空~~")
      if(!this.builingForm.sex) return this.$message.error("请选择党员性别~~")
      if(!this.builingForm.party_user_type_id) return this.$message.error("请选择所属支部~~")
      if(!this.builingForm.summary.trim()) return this.$message.error("党员寄语不可为空~~")
      let data={
        real_name:this.builingForm.real_name,
        city:this.builingForm.city,
        age:this.builingForm.age,
        phone:this.builingForm.phone,
        sex:this.builingForm.sex,
        job:this.builingForm.job,
        party_day:this.builingForm.party_day,
        summary:this.builingForm.summary,
        avatar_url:this.builingForm.avatar_url,
        party_user_type_id:this.builingForm.party_user_type_id,
      }

      if(this.builingForm.$id){
        let id=this.builingForm.$id
        API.editPartyMember(id,data)
          .then(({data}) => {
            this.$message.success("编辑成功！");
          }).catch((err) => {
          }).finally(() =>{
            this.closeDialog()
            this.getList()
          })
      }else{
         API.addPartyMember(data)
          .then(({data}) => {
            this.$message.success("新增成功！");
          }).catch((err) => {
          }).finally(() =>{
            this.closeDialog()
            this.getList()
          })
      }
    },

   // 关闭弹窗
    closeDialog(){
      this.resetForm()
      this.showAddOrEdit=false
      this.showAddOrSolve=false
    },

    resetForm(){
      this.builingForm.$id=""
      this.builingForm.shop_name=""
      this.builingForm.contacts_name=""
      this.builingForm.contacts_phone=""
      this.builingForm.shop_time=""
      this.builingForm.shop_addr=""
      this.builingForm.summary=""
      this.builingForm.banner=""
      this.builingForm.imgs=[]
      this.builingForm.subdistrict_id=1
      this.builingForm.shop_type_id=1
    },
  }


};
</script>

<style lang="scss" scoped>
    ::v-deep{
    .el-message-box{
      min-width:400px;
    }
    .el-dialog{
      margin-top:3vh !important;
    }
    .el-input .el-input__inner{
      border-radius:4px !important;
    }
    .el-switch{
      transform:translateY(6px)
    }
    .el-message-box__message{
      position: absolute !important;
      top:50% !important;
      left:40px !important;
    }
    .el-form-item__label{
      font-size:16px !important;
    }
  }
</style>
